var dataUrl = null;
var pageii = null;
$(function () {
    init();
    $("#generateReport").click(function () {
        generateReport();
    });
    $("#uploadReport").click(function () {
        uploadReport();
    })

});

function init() {
    // dataUrl = $("#dataUrl").val();
    dataUrl ="/modules";

    getProjectName();
    getReportCategory();
    // findReportVersion();

}
function uploadReport() {
    var url =  "/reportManage/uploadReportUI?newDate=" + (new Date().getTime());
    var projectId = $("#projectName").val();
    if (projectId != null && projectId != "" && projectId != undefined) {
        url += "&&projectId=" + projectId;
    }
    var task = $("#task").val();
    if (task != null && task != "" && task != undefined) {
        url += "&&taskId=" + task;
    }else{
        layer.msg("请选择任务！");
        return;
    }
    var reportType = $("#reportType").val();
    if (reportType != null && reportType != "" && reportType != undefined) {
        url += "&&subType=" + reportType;
    }else{
        layer.msg("请选择表名");
        return;
    }
    pageii = layer.open({
        title: "上传报表版本",
        type: 2,
        fixed: true,
        //shadeClose : true,
        area: ["60%", "500px"],
        content:url,
        end: function () {//无论是确认还是取消，只要层被销毁了，end都会执行，不携带任何参数。layer.open关闭事件
            findReportVersion()　　//layer.open关闭刷新
        }
    });
}
/**
 * 获取项目名称
 * @param cropId
 */
function getProjectName() {
    var index = layer.load(1, {shade:  [0.01, '#fff']});
    var url = dataUrl + "/homepage/allProjectByPersonIdAndCorpId?personId=" + $("#personId").val() + "&newDate=" + (new Date().getTime());
    ;
    $.ajax({
        url: url,
        method: "get",
        dataType: "json",
        success: function (result) {
            var html = "";
            html += "<option value=''>------请选择------</option>";
            if (result != null) {
                for (var i = 0; i < result.length; i++) {
                    html += "<option value='" + result[i].id + "'>" + (result[i].caption) + "</option>";
                }
                $("#projectName").html(html);
                $('#projectName').searchableSelect({
                    afterSelectItem:function () {
                        getTaskList(this.holder.data("value"))
                }});

            } else {
                layer.msg("获取列表信息错误，请联系管理员！");
            }
            layer.close(index);
        }
    });
}

/**
 * 获取任务名称
 * @param cropId
 */
function getTaskList(projectId) {
    if (projectId != "") {
        var url = dataUrl + "/basicReport/findByTaskReportList?projectId=" + projectId + "&newDate=" + (new Date().getTime());
        $.ajax({
            url: url,
            method: "get",
            dataType: "json",
            success: function (result) {
                var html = "";
                html += "<option value=''>------请选择------</option>";
                if (result != null) {
                    for (var i = 0; i < result.length; i++) {
                        html += "<option value='" + result[i].id + "'>" + (result[i].caption) + "</option>";
                    }
                    $("#task").html(html);
                } else {
                    layer.msg("获取列表信息错误，请联系管理员！");
                }
            }
        });
    } else {
        var html = "";
        html += "<option value=''>------请选择------</option>";
        $("#task").html(html);
    }

    // findReportVersion();
}

/**
 * 获取报表类型
 * @param cropId
 */
function getReportCategory() {
    var url = dataUrl + "/basicReport/findReportCategory?type=1&newDate=" + (new Date().getTime());
    $.ajax({
        url: url,
        method: "get",
        dataType: "json",
        success: function (result) {
            var html = "";
            html += "<option value=''>----请选择----</option>";
            if (result != null) {
                for (var i = 0; i < result.length; i++) {
                    html += "<option value='" + result[i].id + "'>" + (result[i].subCaption) + "</option>";
                }
                $("#reportType").html(html);
            } else {
                layer.msg("获取列表信息错误，请联系管理员！");
            }
        }
    });
}

function findReportVersion() {
    var url = dataUrl + "/basicReport/findReportVersion?type=1&newDate=" + (new Date().getTime());
    // $("#propName").find("option:selected").attr("data-id");
    var projectId = $("#projectName").val();
    if (projectId != null && projectId != "" && projectId != undefined) {
        url += "&&projectId=" + projectId;
    }
    var task = $("#task").val();
    if (task != null && task != "" && task != undefined) {
        url += "&&taskId=" + task;
    }else {
        return;
    }
    var reportType = $("#reportType").val();
    if (reportType != null && reportType != "" && reportType != undefined) {
        url += "&&subType=" + reportType;
    }
    $.ajax({
        url: url,
        method: "get",
        dataType: "json",
        success: function (result) {debugger
            var html = "";
            if (result != null) {
                if(result.length>0){
                    $("#dataTable").show();
                    // $("#generateReport").hide();
                    for (var i = 0; i < result.length; i++) {
                        html += "  <tr role=\"row\" class=\"odd\">" +
                            "  <th width=\"75px\"><a onclick=\"reportLook('" + result[i].url + "')\">" + result[i].caption + "</a>\n" +
                            "            </th>" +
                            "            <th width=\"150px\">" + crtTimeFtt(result[i].time) + "</th>" +
                            "            <th width=\"150px\">" +
                            "<a class='dropdown-toggle' style='color: #197eb8'  href='javascript:download(\""+result[i].url+"\")'>下载</a>&nbsp;&nbsp;&nbsp;"+
                            "<a class='dropdown-toggle' style='color: red' href='javascript:deleteVersion(\""+result[i].id+"\")'>删除</a>"  +
                            "</th>" +
                            " </tr>";
                    }
                }else{
                    $("#dataTable").hide();
                    // $("#generateReport").show();
                }

                $("#reportVersion").html(html);
            } else {
                layer.msg("获取列表信息错误，请联系管理员！");

            }
        }
    });
}
function deleteVersion(id) {
    var url =  "/reportManage/deleteReportVersion?id="+id+"&newDate=" + (new Date().getTime());
    $.ajax({
        url: url,
        method: "get",
        dataType: "text",
        success: function (result) {
            if (result != null) {
                layer.msg("删除成功")
                findReportVersion()
            } else {
                layer.msg("删除失败！");

            }
        }
    });

}
/**
 * 生成报表
 */
function generateReport() {
    var url = dataUrl + "/basicReport/generateReport?type=1&newDate=" + (new Date().getTime());
    var projectId = $("#projectName").val();
    if (projectId != null && projectId != "" && projectId != undefined) {
        url += "&&projectId=" + projectId;
    }
    var task = $("#task").val();
    if (task != null && task != "" && task != undefined) {
        url += "&&taskId=" + task;
    }else{
        layer.msg("请选择任务！");
        return;
    }
    var reportType = $("#reportType").val();
    if (reportType != null && reportType != "" && reportType != undefined) {
        url += "&&subType=" + reportType;
    }else{
        layer.msg("请选择表名");
        return;
    }
    $.ajax({
        url: url,
        method: "get",
        dataType: "text",
        success: function (result) {
            if(result=='success'){
                layer.msg("生成成功");
                findReportVersion();
            }else{
                findReportVersion();
                // layer.msg("生成失败");
            }
        }
    });
}

/**
 * 预览
 * @param url
 */
function reportLook(url) {
    window.open("https://view.officeapps.live.com/op/view.aspx?src=" + url);
}


/**
 * 下载
 * @param url
 */
function download(url) {
    window.open( url);
}

//创建时间格式化显示
function crtTimeFtt(value, row, index) {
    var crtTime = new Date(value);
    return dateFtt("yyyy-MM-dd hh:mm:ss", crtTime);//直接调用公共JS里面的时间类处理的办法
}

/**************************************时间格式化处理************************************/
function dateFtt(fmt, date) { //author: meizz
    var o = {
        "M+": date.getMonth() + 1,                 //月份
        "d+": date.getDate(),                    //日
        "h+": date.getHours(),                   //小时
        "m+": date.getMinutes(),                 //分
        "s+": date.getSeconds(),                 //秒
        "q+": Math.floor((date.getMonth() + 3) / 3), //季度
        "S": date.getMilliseconds()             //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
